function hrefBack() {
    history.go(-1);
}

var query_data = decodeQuery()
var house_id = query_data['id']
function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

function decodeQuery(){
    var search = decodeURI(document.location.search);
    return search.replace(/(^\?)/, '').split('&').reduce(function(result, item){
        values = item.split('=');
        result[values[0]] = values[1];
        return result;
    }, {});
}

function showErrorMsg() {
    $('.popup_con').fadeIn('fast', function() {
        setTimeout(function(){
            $('.popup_con').fadeOut('fast',function(){}); 
        },1000) 
    });
}

$(document).ready(function(){
    // 获取房屋的基本信息

    var queryData = decodeQuery()
    var id = queryData['id'];
    $.get('/api/v1.0/house/'+ id, function(resp){
        if (resp.errno == '0'){
            $('.house-info > img').attr('src', resp.data.house.img_urls[0])
            $('.house-text > h3').html(resp.data.house.title)
            $('.house-text > p > span').html((resp.data.house.price/100.0).toFixed(2))
        }
    });
    $('.submit-btn').on('click', function(e){
        if($('.order-amount>span').html()){
            $(this).prop('disabled', true)
            var startDate = $('#start-date').val()
            var endDate = $('#end-date').val()
            var data = {
                "house_id":house_id,
                "start_date": startDate,
                "end_date": endDate
            };
            $.ajax({
                url: '/api/v1.0/order',
                data: JSON.stringify(data),
                type: 'post',
                contentType: 'application/json',
                dataType: 'json',
                headers: {
                    'X-CSRFToken': getCookie('csrf_token')
                },
                success: function (res) {
                    if (res.errno=='4101'){
                        location.href= '/login.html'
                    }else if(res.errno =='4004'){
                        showErrorMsg('房间已被预定，请选择其他时间')
                    }else if(res.errno == '0'){
                        location.href = '/orders.html'
                    }
                }
            })
        }
    })



    $(".input-daterange").datepicker({
        format: "yyyy-mm-dd",
        startDate: "today",
        language: "zh-CN",
        autoclose: true
    });
    $(".input-daterange").on("changeDate", function(){
        var startDate = $("#start-date").val();
        var endDate = $("#end-date").val();

        if (startDate && endDate && startDate > endDate) {
            showErrorMsg();
        } else {
            var sd = new Date(startDate);
            var ed = new Date(endDate);
            days = (ed - sd)/(1000*3600*24) + 1;
            var price = $(".house-text>p>span").html();
            var amount = days * parseFloat(price);
            $(".order-amount>span").html(amount.toFixed(2) + "(共"+ days +"晚)");
        }
    });
})
